package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketComment;
import com.cskaoyan.market.service.MarketCommentService;
import com.cskaoyan.market.service.impl.MarketCommentServiceIml;
import com.cskaoyan.market.util.GetRequestBody;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author lanch
 * @since 2023/05/08 19:07
 */
@WebServlet("/admin/comment/*")
public class AdminCommentController extends HttpServlet {
    private MarketCommentService commentService = new MarketCommentServiceIml();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/comment/", "");
        if("list".equals(op)){
            list(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/comment/", "");
        if("delete".equals(op)){
            delet(req,resp);
        }
        
    }
    //删除一条评论
    private void delet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        MarketComment reqBodyObject = (MarketComment)GetRequestBody.getReqBodyObject(req, new MarketComment());
        commentService.deletComment(reqBodyObject);
        Object ok = ResponseUtil.ok();
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ok));
    }

/*    //获取评论列表
    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        List<MarketComment> commentsList = commentService.getCommentsList(page, limit, sort, order);
        Object okList = ResponseUtil.okList(commentsList);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(okList));

    }*/
    //获取评论列表
    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        String userId = req.getParameter("userId");
        String valueId = req.getParameter("valueId");
        List<MarketComment> commentsList = commentService.getCommentsList(page, limit, sort, order,userId,valueId);
        Object okList = ResponseUtil.okList(commentsList);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(okList));

    }
}
